const express = require('express')
const fs = require('fs')
const path = require('path')
const router = express.Router()
router.get('/list', (req, res) => {
    console.log('=======教师模块>教师列表============');
    fs.readFile(path.join(__dirname, "../", "data", "teachers.json"), "utf-8", (err, data) => {
        res.send({
            code: 1,
            data: {
                total: JSON.parse(data).length,
                rows: JSON.parse(data)
            }
        })
    })

})
router.get('/search', (req, res) => {
    fs.readFile(path.join(__dirname, "../", "data", "teachers.json"), "utf-8", (err, data) => {
        let teachers = JSON.parse(data)
        let result = teachers.filter(item => item.tname.indexOf(req.query.name) != -1)
        res.send({
            code: 1,
            data: {
                total: result.length,
                rows: result
            }
        })
    })
})
/* 
    按教师编号删除教师信息
    删除功能在router路由对象的delete
*/
router.delete("/del/:id", (req, res) => {
    //使用req.params来接收动态参数
    console.log('------删除教师---------');
    console.log(req.params);
    fs.readFile(path.join(__dirname, "../", "data", "teachers.json"), "utf-8", (err, data) => {
        let teachers = JSON.parse(data)
        //过滤掉请求的ID的信息
        teachers = teachers.filter(item => item.tid != req.params.id)
        //将过滤后的数组信息重新写到文件上
        fs.writeFile(path.join(__dirname, "../", "data", "teachers.json"), JSON.stringify(teachers), (err, data) => {
            if (!err) {
                //响应数据到前端
                res.send({
                    code: 1,
                    message: `${req.params.id}已经被成功删除`,
                    data: {
                        rows: teachers
                    }
                })
            }
        })
    })

})

/* 
    增加操作的请求方式一般都是为POST请求
*/
router.post('/add', (req, res) => {
    //获取body请求参数
    fs.readFile(path.join(__dirname, "../", "data", "teachers.json"), "utf-8", (err, data) => {
        let teachers = JSON.parse(data)
        let result = teachers.filter(item => item.tid == req.body.tid)
        if (result.length > 0) {
            res.send({
                code: 0,
                message: '用户已存在'
            })
        } else {
            teachers.push(req.body)
            fs.writeFile(path.join(__dirname, "../", "data", "teachers.json"), JSON.stringify(teachers), (err, data) => {
                if (!err) {
                    //响应数据到前端
                    res.send({
                        code: 1,
                        message: `增加成功`,
                        data: {
                            rows: teachers
                        }
                    })
                }
            })
        }
    })
})

/* 
   按ID查询教师信息
*/
router.get('/byid/:id',(req,res)=>{
    fs.readFile(path.join(__dirname, "../", "data", "teachers.json"), "utf-8", (err, data) => {
        let teachers = JSON.parse(data)
        let result=teachers.filter(item=>item.tid==req.params.id)
        if(result){
            if(result.length>0){
                res.send({
                    code:1,
                    message:'查询成功',
                    data:result[0]
                })
            }else{
                res.send({
                    code:0,
                    message:'查询失败'
                })
            }
        }
    })
})

/* 
    修改功能
*/
router.put('/modify',(req,res)=>{
    fs.readFile(path.join(__dirname, "../", "data", "teachers.json"), "utf-8", (err, data) => {
      if(!err){
        //从文件中读取教师集合信息
        let teachers=JSON.parse(data);
        //获取body参数
        let {tid}=req.body
        //要找到指定的tid在数组中的下标位置
        //记录要查询元素在数组中的下标位置，这里使用count来记录
        let count=0
        teachers.forEach((element,index) => {
            if(element.tid===tid){
                count=index
            }
        });
        //使用splice方法完成替换功能
        teachers.splice(count,1,req.body)
        fs.writeFile(path.join(__dirname, "../", "data", "teachers.json"), JSON.stringify(teachers), (err, data) => {
            if (!err) {
                //响应数据到前端
                res.send({
                    code: 1,
                    message: `修改成功`,
                    data: {
                        rows: teachers
                    }
                })
            }
        })
      }
    })
})
module.exports = router